package com.example.demo.db;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author lzz
 * @package com.example.demo.mysql.db
 * @create 2019-05-22 9:22
 * @description: ${DESCRIPTION}
 */
@Component
public class UserDB {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public String toekn(String token,String account){
        String sql = "select * from user where account =\""+account+"\"";
        String sqlupdate = "update  user set token =\""+token+"\" where account =\" "+account+"\"";
        Map user = jdbcTemplate.queryForList(sql).get(0);
        String tokenRe="";
        if(user.get("token")!=null && !"".equals(user.get("token"))){
            tokenRe= (String) user.get("token");
        }else {
            tokenRe=token;
        }
        jdbcTemplate.update(sqlupdate);
        return tokenRe;
    }

    public Map getUserByAccount(String account){
        String sql = "select * from user where account =  \""+account+"\"";
        Map user = jdbcTemplate.queryForList(sql).get(0);
        return user;
    }
    public String getUserTokenByAccount(String account){
        String sql = "select * from user where account =  \""+account+"\"";
        String user = (String) jdbcTemplate.queryForList(sql).get(0).get("token");
        return user;
    }
    public Boolean Login(String account,String password){
        String sql = "select * from user where account= \""+account+"\"";
        System.out.println(jdbcTemplate.queryForList(sql).get(0));
        Map user =  jdbcTemplate.queryForList(sql).get(0);

        if(password.equals(user.get("password"))){
            return true;
        }
        return false;
    }
}
